﻿using System;

namespace Miao
{
    /// <summary>
    /// 表示可设置的值
    /// </summary>
    public class TValue<T>
    {
        public event Action<T> onValueChanged;

        public T defaultValue { get; private set; }
        public T value { get => _value; set => SetValue(value); }

        private T _value;

        public TValue(T defaultValue)
        {
            this.defaultValue = _value = defaultValue;
        }

        public void Reset()
        {
            value = defaultValue;
        }
        public void SetValueWithoutNotify(T newValue)
        {
            _value = newValue;
        }

        public override string ToString()
        {
            return value.ToString();
        }

        virtual protected void SetValue(T newValue)
        {
            _value = newValue;
            onValueChanged?.Invoke(value);
        }

        public static implicit operator T(TValue<T> self)
        {
            return self.value;
        }
    }
}